﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using Granite.Xaml;
using Toon.Models.Characters;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Collections.Generic;
using Toon.Models.Catalogs;
using Toon.Views;

namespace Toon
{
	public class PrimaryViewModel : NavigationViewModel
	{
		/// <summary>
		/// Initializes a new instance of the PrimaryViewModel class.
		/// </summary>
		public PrimaryViewModel(PhoneApplicationFrame frame)
			: base(frame)
		{
			CurrentRuleSet = new StandardRules(new StandardCatalog());
		}

		public ICommand OpenToonWebsiteCommand
		{
			get { return GetCommand(OpenToonWebsite, "OpenToonWebsiteCommand"); }
		}

		public void OpenToonWebsite()
		{
			OpenWebsite(@"http://www.sjgames.com/toon/");
		}

		public Character CurrentCharacter
		{
			get { return Get<Character>("CurrentCharacter"); }
			set { Set(value, "CurrentCharacter"); }
		}

		public RuleSet CurrentRuleSet
		{
			get { return Get<RuleSet>("CurrentRuleSet"); }
			set { Set(value, "CurrentRuleSet"); }
		}

		public ICommand CreateNewCommand
		{
			get { return GetCommand(CreateNew, "CreateNewCommand"); }
		}

		public void CreateNew()
		{
			CurrentCharacter = CurrentRuleSet.CreateNewCharacter();
			Navigate(typeof(NewCharacter));
		}

		public ICommand CreateRandomCommand
		{
			get { return GetCommand(CreateRandom, "CreateRandomCommand"); }
		}

		public void CreateRandom()
		{
			CurrentCharacter = CurrentRuleSet.CreateRandomCharacter();
			Navigate(typeof(NewCharacter));
		}

		public ICommand SaveCommand
		{
			get { return GetCommand(Save, "SaveCommand"); }

		}

		public void Save()
		{
			//var xml = CurrentCharacter.ToXml();

			//var dlg = new SaveFileDialog();
			//dlg.DefaultExt = ".toon-character";
			//dlg.Filter = "Toon Character (*.toon-character)|*.toon-character|Text File (*.txt)|*.txt";
			//dlg.DefaultFileName = CurrentCharacter.GetSuggestedFileName();

			//if (dlg.ShowDialog() == true)
			//{
			//	CurrentCharacter.FileName = dlg.SafeFileName;
			//	using (var stream = new StreamWriter(dlg.OpenFile()))
			//	{
			//		if (dlg.FilterIndex == 1)
			//			stream.Write(xml);
			//		if (dlg.FilterIndex == 2)
			//			stream.Write("Hi, I'm a text file!");

			//		CurrentCharacter.AcceptChanges();
			//	}
			//}
		}

		public ICommand OpenCommand
		{
			get { return GetCommand(Open, "OpenCommand"); }

		}

		public void Open()
		{
			//try
			//{
			//	var dlg = new OpenFileDialog();
			//	dlg.Filter = "Toon Characters (*.toon-character)|*.toon-character|All Files (*.*)|*.*";
			//	if (dlg.ShowDialog() == true)
			//	{
			//		using (var stream = dlg.File.OpenText())
			//		{
			//			var xml = stream.ReadToEnd();
			//			CurrentCharacter = new Character(XElement.Parse(xml));
			//			CurrentCharacter.FileName = dlg.File.Name;
			//		}
			//	}

			//	Navigate("EditCharacter");
			//}
			//catch
			//{
			//	MessageBox.Show("Unable to open file");
			//	return;
			//}

		}

		public ICommand RestartCommand
		{
			get { return GetCommand(Restart, "RestartCommand"); }
		}

		public void Restart()
		{
			Navigate(typeof(Home));
		}

		/// <summary>
		/// Save application state
		/// </summary>
		/// <param name="state"></param>
		public void Deactivated(IDictionary<string, object> state)
		{
			if (CurrentCharacter != null)
			{
				state["CurrentCharacter"] = CurrentCharacter.ToXml().ToString();
				state["CurrentFileName"] = CurrentCharacter.FileName;
			}
			else
			{
				state["CurrentCharacter"] = null;
				state["CurrentFileName"] = null;
			}
		}

		/// <summary>
		/// Load application state
		/// </summary>
		/// <param name="state"></param>
		public void Activated(IDictionary<string, object> state)
		{
			if (state.ContainsKey("CurrentCharacter") && state["CurrentCharacter"] != null)
			{
				CurrentCharacter = new Character(XElement.Parse((string)state["CurrentCharacter"]));

				if (state.ContainsKey("CurrentFileName"))
					CurrentCharacter.FileName = (String)state["CurrentFileName"];
			}
		}
	}


}



